package com.sdygmedia.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.domain.Category;
import com.sdygmedia.service.CategoryChildService;
import com.sdygmedia.service.CategoryService;

@Controller
public class CategoryAction extends ActionSupport {

	private Category category;
	@Resource
	private CategoryService categoryService;
	
	public String addUI() throws Exception {
		return "addUI";
	}
	
	public String add() throws Exception {
		categoryService.save(getCategory());
		return "add";
	}
	
	public String delete() throws Exception {
		categoryService.delete(category.getId());
		return "toList";
	}
	
	public String updateUI() throws Exception {
		Category categoryUp = categoryService.getById(category.getId());
		ActionContext.getContext().put("categoryUp", categoryUp);
		return "updateUI";
	}
	
	public String update() throws Exception {
		category = categoryService.getById(category.getId());
		categoryService.update(category);
		return "update";
	}
	
	public String list() throws Exception {
		List categoryList = categoryService.findAll();
		ActionContext.getContext().put("categoryList", categoryList);
		
		/*for (Iterator<Category> it = categoryList.iterator(); it.hasNext(); ){
			Category c = it.next();
			System.out.println(c.getCategoryChilds().size());
		}*/
		
		return "list";
	}

	
	//-----------------------------------------
	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}
}
